package com.tongyi.modules.app.controller;


/**
 * 小程序验证服务器
 *
 * @author 林佛权
 */
//@Slf4j
//@RestController
//@RequestMapping("/app/wx/ma/portal")
public class WxMaPortalController {
//    private final Logger logger = LoggerFactory.getLogger(this.getClass());
//    @Autowired
//    private WxMaService wxMaService;
//    @Autowired
//    private WxMaMessageRouter wxMaMessageRouter;
//
//    @GetMapping(produces = "text/plain;charset=utf-8")
//    public String authGet(@RequestParam(name = "signature", required = false) String signature,
//                          @RequestParam(name = "timestamp", required = false) String timestamp,
//                          @RequestParam(name = "nonce", required = false) String nonce,
//                          @RequestParam(name = "echostr", required = false) String echostr) {
//        this.logger.info("\n接收到来自微信服务器的认证消息：signature = [{}], timestamp = [{}], nonce = [{}], echostr = [{}]",
//                signature, timestamp, nonce, echostr);
//
//        if (StringUtils.isAnyBlank(signature, timestamp, nonce, echostr)) {
//            throw new IllegalArgumentException("请求参数非法，请核实!");
//        }
//
//        if (wxMaService.checkSignature(timestamp, nonce, signature)) {
//            return echostr;
//        }
//
//        return "非法请求";
//    }
//
//    @PostMapping(produces = "application/xml; charset=UTF-8")
//    public String post(@RequestBody String requestBody,
//                       @RequestParam(name = "msg_signature", required = false) String msgSignature,
//                       @RequestParam(name = "encrypt_type", required = false) String encryptType,
//                       @RequestParam(name = "signature", required = false) String signature,
//                       @RequestParam("timestamp") String timestamp,
//                       @RequestParam("nonce") String nonce) {
//        this.logger.info("\n接收微信请求：[msg_signature=[{}], encrypt_type=[{}], signature=[{}]," +
//                        " timestamp=[{}], nonce=[{}], requestBody=[\n{}\n] ",
//                msgSignature, encryptType, signature, timestamp, nonce, requestBody);
//
//        final boolean isJson = Objects.equals(wxMaService.getWxMaConfig().getMsgDataFormat(),
//                WxMaConstants.MsgDataFormat.JSON);
//        if (StringUtils.isBlank(encryptType)) {
//            // 明文传输的消息
//            WxMaMessage inMessage;
//            if (isJson) {
//                inMessage = WxMaMessage.fromJson(requestBody);
//            } else {//xml
//                inMessage = WxMaMessage.fromXml(requestBody);
//            }
//
//            this.route(inMessage);
//            return "success";
//        }
//
//        if ("aes".equals(encryptType)) {
//            // 是aes加密的消息
//            WxMaMessage inMessage;
//            if (isJson) {
//                inMessage = WxMaMessage.fromEncryptedJson(requestBody, wxMaService.getWxMaConfig());
//            } else {//xml
//                inMessage = WxMaMessage.fromEncryptedXml(requestBody, wxMaService.getWxMaConfig(),
//                        timestamp, nonce, msgSignature);
//            }
//
//            this.route(inMessage);
//            return "success";
//        }
//
//        throw new RuntimeException("不可识别的加密类型：" + encryptType);
//    }
//
//    private void route(WxMaMessage message) {
//        try {
//            this.wxMaMessageRouter.route(message);
//        } catch (Exception e) {
//            this.logger.error(e.getMessage(), e);
//        }
//    }

}
